﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using BGCOP.CAVTOP.BUSINESSLAYER;

namespace BGCOP.CAVTOP.WINFORMS
{
    
    public partial class frmAsignaParqueo : Form
    {
        AutoCompleteStringCollection txtC;
        AutoCompleteStringCollection txtCp;
        AutoCompleteStringCollection txtC3;
        AutoCompleteStringCollection txtC4;
        bool agrego;

        public frmAsignaParqueo()
        {
            agrego = false;
            InitializeComponent();
        }

        private void frmAsignaParqueo_Load(object sender, EventArgs e)
        {
            llenaLista();
        }
        public static AutoCompleteStringCollection LoadAutoCompletePlaca(List<viParqueoVehiculo> ao)
        {
            AutoCompleteStringCollection stringCol = new AutoCompleteStringCollection();
            foreach (var t in ao)
            {
                stringCol.Add(t.Placa);
            }
            return stringCol;
        }
        public static AutoCompleteStringCollection LoadAutoCompleteParqueo(List<viParqueoVehiculo> ao)
        {
            AutoCompleteStringCollection stringCol = new AutoCompleteStringCollection();
            foreach (var t in ao)
            {
                stringCol.Add(t.Parqueadero);
            }
            return stringCol;
        }
        public static AutoCompleteStringCollection LoadAutoCompletetVehi(List<TVehiculo> ao)
        {
            AutoCompleteStringCollection stringCol = new AutoCompleteStringCollection();
            foreach (var t in ao)
            {
                stringCol.Add(t.Placa);
            }
            return stringCol;
        }
        public static AutoCompleteStringCollection LoadAutoCompletetPar(List<TParqueo> ao)
        {
            AutoCompleteStringCollection stringCol = new AutoCompleteStringCollection();
            foreach (var t in ao)
            {
                stringCol.Add(t.nParqueo);
            }
            return stringCol;
        }
        public void llenaLista()
        {
            Vehiculos vei = new Vehiculos();
            Parqueaderos pr = new Parqueaderos();
            List<TVehiculo> lve = vei.ListarTodos();
            List<TParqueo> lprq = pr.ListarTodos();
            txtC3 = LoadAutoCompletetVehi(lve);
            txtC4 = LoadAutoCompletetPar( lprq );
            
            comboBox3.DataSource = lve;
            comboBox3.DisplayMember = "Placa";
            comboBox3.ValueMember = "idVehiculo";
            comboBox4.DataSource = lprq;
            comboBox4.DisplayMember = "nparqueo";
            comboBox4.ValueMember = "idParqueadero";
            comboBox3.SelectedIndex = -1;
            comboBox4.SelectedIndex = -1;
            
            comboBox3.AutoCompleteCustomSource = txtC3;
            comboBox3.AutoCompleteMode = AutoCompleteMode.Suggest;
            comboBox3.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox4.AutoCompleteCustomSource = txtC4;
            comboBox4.AutoCompleteMode = AutoCompleteMode.Suggest;
            comboBox4.AutoCompleteSource = AutoCompleteSource.CustomSource;


            VehiculosParqueos vp = new VehiculosParqueos();
            List<viParqueoVehiculo> lista = vp.listarTodos(); 
            txtC = LoadAutoCompletePlaca(lista);
            txtCp = LoadAutoCompleteParqueo(lista);

            dataGridView1.DataSource = lista;
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCellsExceptHeader;
            this.dataGridView1.Columns["Placa"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

            //this.dataGridView1.AlternatingRowsDefaultCellStyle 

            this.dataGridView1.Columns["Lunes"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            
            
            //dataGridView1.
            //this.dataGridView1.Columns["Lunes"]
            
            this.dataGridView1.Columns["Martes"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns["Miercoles"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns["Jueves"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns["Viernes"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns["Sábado"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns["Domingo"].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
            this.dataGridView1.Columns["idVehiculo"].Visible = false;
            this.dataGridView1.Columns["idParqueadero"].Visible = false;
            //dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.Fill);

            comboBox1.AutoCompleteCustomSource = txtC;
            comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.DataSource = lista;
            comboBox1.DisplayMember = "Placa";
            comboBox1.SelectedIndex = -1;

            comboBox2.AutoCompleteCustomSource = txtCp;
            comboBox2.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            comboBox2.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox2.DataSource = lista;
            comboBox2.DisplayMember = "Parqueadero";
            comboBox2.SelectedIndex = -1;
            lblIdVehiculo.Text = "";
            lblIdParqueo.Text = "";

            dataGridView1.Rows[0].Selected = true;
            selectGrid();

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            //acaba de cambiar la placa debo buscarala en el combo 3
            comboBox3.Text = comboBox1.Text;
            
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox4.Text = comboBox2.Text;
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            selectGrid();
        }

        public void selectGrid()
        {
            try
            {
                checkBox1.Checked = Convert.ToBoolean(dataGridView1.SelectedRows[0].Cells["Lunes"].Value);
                checkBox2.Checked = Convert.ToBoolean(dataGridView1.SelectedRows[0].Cells["Martes"].Value);
                checkBox3.Checked = Convert.ToBoolean(dataGridView1.SelectedRows[0].Cells["Miercoles"].Value);
                checkBox4.Checked = Convert.ToBoolean(dataGridView1.SelectedRows[0].Cells["Jueves"].Value);
                checkBox5.Checked = Convert.ToBoolean(dataGridView1.SelectedRows[0].Cells["Viernes"].Value);
                checkBox6.Checked = Convert.ToBoolean(dataGridView1.SelectedRows[0].Cells["Sábado"].Value);
                checkBox7.Checked = Convert.ToBoolean(dataGridView1.SelectedRows[0].Cells["Domingo"].Value);
                lblIdVehiculo.Text = dataGridView1.SelectedRows[0].Cells["idVehiculo"].Value.ToString();
                lblIdParqueo.Text = dataGridView1.SelectedRows[0].Cells["idParqueadero"].Value.ToString();
            }
            catch (Exception ex)
            {

                //MessageBox.Show(ex.Message);
            }
            
        }

        private void btnCancelar_Click(object sender, EventArgs e)
        {
            agrego = false;
            this.Close();
        }

        private void btnAgregar_Click(object sender, EventArgs e)
        {
            agrego = true;
            lblIdVehiculo.Text = "";
            lblIdParqueo.Text = "";
            comboBox3.SelectedIndex = -1;
            comboBox4.SelectedIndex = -1;
            checkBox1.Checked = false;
            checkBox2.Checked = false;
            checkBox3.Checked = false;
            checkBox4.Checked = false;
            checkBox5.Checked = false;
            checkBox6.Checked = false;
            checkBox7.Checked = false;
        }

        private void btnModificar_Click(object sender, EventArgs e)
        {
            try
            {
                if (agrego == true)
                {
                    using (masterDBACEntities db = new masterDBACEntities())
                    {
                        TvehiculoParqueo tabla = new TvehiculoParqueo()
                        {

                            idVehiculo = (int)comboBox3.SelectedValue,
                            idParqueadero = (int)comboBox4.SelectedValue,
                            diaSemana = 1,
                            dia1 = checkBox1.Checked,
                            dia2 = checkBox2.Checked,
                            dia3 = checkBox3.Checked,
                            dia4 = checkBox4.Checked,
                            dia5 = checkBox5.Checked,
                            dia6 = checkBox6.Checked,
                            dia7 = checkBox7.Checked,
                            ObservacionesVP = "Sin Observaciones",
                            fechaUltimaGestion = DateTime.Now,
                            usuarioUltimaGestion = (int)BGCOP.CAVTOP.WINFORMS.Program.idUsuarioActual

                        };
                        if (MessageBox.Show("Desea confirmar la operacion?", Properties.Settings.Default.Titulo,
                                            MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
                        {
                            db.AddToTvehiculoParqueo(tabla);
                            db.SaveChanges();
                            llenaLista();
                        }
                    }
                }
                else
                {
                    // es una modificacion
                    
                        VehiculosParqueos vp = new VehiculosParqueos();
                        vp.Modificar((int) comboBox4.SelectedValue, (int) comboBox3.SelectedValue, DateTime.Now,
                                     (int) BGCOP.CAVTOP.WINFORMS.Program.idUsuarioActual, 1, "Sin Observaciones",
                                     checkBox1.Checked, checkBox2.Checked, checkBox3.Checked, checkBox4.Checked,
                                     checkBox5.Checked, checkBox6.Checked, checkBox7.Checked);

                }
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            
        }

        private void frmAsignaParqueo_CausesValidationChanged(object sender, EventArgs e)
        {
            //MessageBox.Show("eere");
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblIdVehiculo.Text = comboBox3.SelectedValue.ToString();
                lblIdParqueo.Text = comboBox4.SelectedValue.ToString();
            }
            catch (Exception)
            {
                
                
            }
            
        }

        private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                lblIdVehiculo.Text = comboBox3.SelectedValue.ToString();
                lblIdParqueo.Text = comboBox4.SelectedValue.ToString();
            }
            catch (Exception)
            {


            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Desea Eliminar el registro seleccionado?", Properties.Settings.Default.Titulo,
                                            MessageBoxButtons.YesNoCancel) == DialogResult.Yes)
            {
                VehiculosParqueos vp = new VehiculosParqueos();
                vp.Eliminar((int) comboBox4.SelectedValue, (int) comboBox3.SelectedValue);
            }
        }
    }
}
